// ignore_for_file: depend_on_referenced_packages

import 'package:flutter_2b_client/model/IntIdAndName.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:retrofit/http.dart';
import 'package:dio/dio.dart';

import '../utils/Page.dart';
import 'Initial.dart';

part 'MarketApi.g.dart';

part 'MarketApi.freezed.dart';

@RestApi()
abstract class MarketApi {

  factory MarketApi(Dio dio, {String baseUrl}) = _MarketApi;

  @GET("markets")
  Future<Page<QueryMarketsResponse>> _queryMarkets(
      @Queries() Map<String, dynamic> queries);

  @GET("markets/{id}")
  Future<MarketDetail> getMarketDetail(@Path("id") int id);

  @GET("markets?projection=summary")
  Future<Page<IntIdAndName>> queryMarketSummary(
      @Query("keyword") String? keyword,
      @Queries() Map<String, dynamic> queries);
}

extension MarketApiExtension on MarketApi {
  Future<Page<QueryMarketsResponse>> queryMarkets(
      QueryMarketRequest request, Pageable pageable) {
    pageable.toRequestParamsMap();
    return _queryMarkets(pageable.toRequestParamsMap());
  }
}

@freezed
class QueryMarketRequest with _$QueryMarketRequest {
  const factory QueryMarketRequest({
    String? name,
  }) = _QueryMarketRequest;

  const QueryMarketRequest._();

  factory QueryMarketRequest.fromJson(Map<String, dynamic> json) =>
      _$QueryMarketRequestFromJson(json);
}

@freezed
class MarketDetail with _$MarketDetail {
  const factory MarketDetail({
    required int id,
    required String name,
    IntIdAndName? saleManager,
  }) = _MarketDetail;

  const MarketDetail._();

  factory MarketDetail.fromJson(Map<String, dynamic> json) =>
      _$MarketDetailFromJson(json);
}

@freezed
class QueryMarketsResponse with _$QueryMarketsResponse {
  const factory QueryMarketsResponse({
    required int id,
    required String name,
  }) = _QueryMarketsResponse;

  const QueryMarketsResponse._();

  factory QueryMarketsResponse.fromJson(Map<String, dynamic> json) =>
      _$QueryMarketsResponseFromJson(json);
}

MarketApi marketApi = MarketApi(dio, baseUrl: baseUrl);
